using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
namespace GoogleEarthClasses.Global
{
    public class DBHelper
    {
        const string FileConfig = "GE.config.txt";
        /// <summary>
        /// Global connection to database
        /// </summary>
        /// <param name="connStringKey">Connection Strings Key from Settings1.settings that the user wants to get</param>
        /// <returns>Connection to database</returns>
        public static SqlConnection GetConnection(string connStringKey)
        {
            // global connection string
            //string ConnString = ConfigurationSettings.AGoogleEarthClassesSettings[connStringKey];
            string ConnString = System.Configuration.ConfigurationSettings.AppSettings[connStringKey];
            try
            {
                //SqlConnection Conn = new SqlConnection(ConnString);
                SqlConnection Conn = new SqlConnection(connStringKey);
                if (Conn.State != ConnectionState.Open)
                    Conn.Open();
                return Conn;
            }
            catch
            {
                throw;
            }
        }
        public static SqlConnection GetConnection()
        {
            //return GetConnection(getLocalConnection());
            return GetConnection(getMasterStringConnection());
        }

        public static SqlParameter CreateParameter(string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, byte precision, byte scale, object value)
        {
            return new SqlParameter(parameterName, dbType, size, direction, isNullable, precision, scale, String.Empty, DataRowVersion.Default, value);
        }
        public static SqlParameter CreateParameter(string parameterName, SqlDbType dbType, int size, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, dbType, size, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateIntParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Int, 0, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateBitParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Bit, 0, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateDateTimeParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.DateTime, 0, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateTextParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            if (value == null)
                return CreateParameter(parameterName, SqlDbType.Text, 0, direction, true, 0, 0, DBNull.Value);
            else
                return CreateParameter(parameterName, SqlDbType.Text, value.ToString().Length + 1, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateCharParameter(string parameterName, int size, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Char, size, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateVarCharParameter(string parameterName, int size, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.VarChar, size, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateNVarCharParameter(string parameterName, int size, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.NVarChar, size, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateRealParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Real, 0, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateDecimalParameter(string parameterName, ParameterDirection direction, bool isNullable, byte precision, byte scale, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Decimal, 0, direction, isNullable, precision, scale, value);
        }
        public static SqlParameter CreateFloatParameter(string parameterName, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Float, 0, direction, isNullable, 0, 0, value);
        }
        public static SqlParameter CreateImageParameter(string parameterName, int size, ParameterDirection direction, bool isNullable, object value)
        {
            return CreateParameter(parameterName, SqlDbType.Image, size, direction, isNullable, 0, 0, value);
        }

        public static string getLocalConnection()
        {
            //string[] Connection = File.ReadAllLines(FileConfig);
            //return Connection[0];
            return Constants.K_ServerConnection;
        }

        protected static string getMasterStringConnection()
        {
            return Constants.K_ServerConnection;
        }

        public static SqlConnection GetMasterConnection()
        {
            return GetConnection(getMasterStringConnection());
        }

        public static bool TestMasterConnection()
        {
            string Connection = getMasterStringConnection();
            SqlConnection Conn = new SqlConnection(Connection);
            try
            {
                if (Conn.State == ConnectionState.Closed)
                    Conn.Open();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            finally
            {
                Conn.Close();
            }
        }

    }

}